//
//  Refresh+Dev.swift
//  CodingTest
//
//  Created by LYPC on 2021/11/18.
//

import UIKit
import MJRefresh

extension UITableView {
    ///  给 tableView 添加 refreshHeader
    /// - Parameter isIpxTop: 是否是iPhoneX且无tableview占据整个页面
    public func addRefreshHeader(isIpxTop: Bool = false, block:@escaping () -> Void) {
        let header = CTRefreshHeader(refreshingBlock: block)
        header.isIpxTop = isIpxTop
        setRefreshHeader(header)
        mj_header = header
    }
    ///  给 tableView 添加 refreshFooter
    public func addRefreshFooterV(_ noMoreText: NSAttributedString? = nil, block:@escaping () -> Void) {
        let footer = CTRefreshFooter(refreshingBlock: block)
        setRefreshFooter(footer)
        mj_footer = footer
        guard let text = noMoreText else { return }
        footer.setNoMoreTitle(text)
    }
    
    ///  给 tableView 添加 refreshHeader
    /// - Parameter:
    ///     - withBackgroundColor: header的背景色
    ///     - isIpxTop: 是否是iPhoneX且无tableview占据整个页面
    public func addRefreshHeader(withBackgroundColor bgColor: UIColor,
                                 indicatorTint: UIColor? = nil,
                                 isIpxTop: Bool = false,
                                 block:@escaping () -> Void) {
        let header = CTRefreshHeader(refreshingBlock: block)
        header.isIpxTop = isIpxTop
        header.bgViewColor = bgColor
        header.indicatorTintColor = indicatorTint
        setRefreshHeader(header)
        mj_header = header
    }
    
    // MARK: - ********* Private Method
    private func setRefreshHeader(_ header:CTRefreshHeader) {
        header.isAutomaticallyChangeAlpha = true
    }
    private func setRefreshFooter(_ footer:CTRefreshFooter) {
        
    }
}
